import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    redirect:'/welcome',
    component: () => import('@/views/home'),
    children:[
      {
        name:'welcome',
        path:'/welcome',
        component: () => import('@/views/welcome')
      }
    ]
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/Login')
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to,要去哪的路由信息对象，to.path可以获得要去哪的路由锚点信息
  // from 表示从哪来的路由信息对象，from.path表示从哪来的路由锚点信息
  // next是回调函数，没有特殊情况都会执行
  // 
  var token = window.sessionStorage.getItem('token')
  if (!token && to.path !== '/login') {
    return next('/login')
  }
  next()
})
export default router
